package com.dapeng.cleardatabackend.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.boot.jackson.JsonComponent;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import java.util.TimeZone;

/**
 * Spring MVC Json 配置
 * @author dapeng
 */
@JsonComponent
public class JsonConfig {


    /**
     * 添加 Long 转 json 精度丢失的配置
     *
     * @param builder 建设者
     * @return {@link ObjectMapper }
     */
    @Bean
    public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
        ObjectMapper objectMapper = builder.createXmlMapper(false).build();
        SimpleModule module = new SimpleModule();
        module.addSerializer(Long.class, ToStringSerializer.instance);
        module.addSerializer(Long.TYPE, ToStringSerializer.instance);
        objectMapper.registerModule(module);
        return objectMapper;
    }

    // /**
    //  * 设置时区
    //  *
    //  * @return {@link Jackson2ObjectMapperBuilderCustomizer }
    //  */
    // @Bean
    // public Jackson2ObjectMapperBuilderCustomizer jacksonCustomizer() {
    //     return builder -> builder.timeZone(TimeZone.getTimeZone("GMT+8"));
    // }
}